﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;
using System.IO;
using System.Configuration;
using System.Web.UI.HtmlControls;

namespace JohnnySins.Admin
{
    public partial class MyLife : BasePage
    {
        public string CalendarJS = "";
        protected string PathChat
        {
            get
            {
                return ResolveClientUrl("~/Files/Archives/Chat/");
            }
        }
        protected string PathVideo 
        {
            get
            {
                return ResolveClientUrl("~/Files/Archives/Stream/");
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }
        
        protected void upCalendar_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["__EVENTTARGET"]))
                if (Request["__EVENTTARGET"] != upCalendar.ClientID)
                    return;

            int month = DateTime.Today.Month;
            int year = DateTime.Today.Year;
            if (!string.IsNullOrEmpty(Request["__EVENTARGUMENT"]))
            {
                string[] arrMonthYear = Request["__EVENTARGUMENT"].Split(';');
                month = Convert.ToInt32(arrMonthYear[0]) + 1;
                year = Convert.ToInt32(arrMonthYear[1]);
            }

            DateTime begDate = new DateTime(year, month, 1);
            DateTime endDate = begDate.AddMonths(1);

            DirectoryInfo info = new DirectoryInfo(Server.MapPath(PathVideo));
            List<FileInfo> files = info.GetFiles("*.flv", SearchOption.TopDirectoryOnly).Where(p => p.CreationTime >= begDate).Where(p => p.CreationTime < endDate).ToList();
            info = new DirectoryInfo(Server.MapPath(PathChat));
            files.AddRange(info.GetFiles("*.txt", SearchOption.TopDirectoryOnly).Where(p => p.CreationTime >= begDate).Where(p => p.CreationTime < endDate).ToList());

            CalendarJS = "$(\".responsive-calendar\").responsiveCalendar({";
            CalendarJS += "time: \"" + year.ToString() + "-" + month.ToString("00") + "\", ";
            CalendarJS += "events: {";
            bool first = true;
            for (DateTime currDate = begDate; currDate < endDate; currDate = currDate.AddDays(1))
            {
                int count = files.Where(p => p.CreationTime >= currDate).Where(p => p.CreationTime < currDate.AddDays(1)).Count();
                if (count > 0)
                {
                    if (!first)
                        CalendarJS += ", ";
                    else
                        first = false;

                    CalendarJS += string.Format("\"{0}\": {{\"number\": {1}}}", currDate.ToString("yyyy-MM-dd"), count.ToString());
                }
            }
            CalendarJS += "}, ";
            CalendarJS += @"onActiveDayClick: function (events) {
                                                            var key;
                                                            key = $(this).data('day') + ';' + $(this).data('month') + ';' + $(this).data('year');
                                                            selectedDate = key;
                                                            RefreshArchive(key);
                                                        },";
            CalendarJS += @"onMonthChange: function (events) {
                                                            var key;
                                                            selectedDate = key;
                                                            key = String($(this)[0].currentMonth) + ';' + String($(this)[0].currentYear);
                                                            RefreshCalendar(key);
                                                        }";
            CalendarJS += "});";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Calendar", CalendarJS, true);
        }

        protected void upArchive_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["__EVENTTARGET"]))
                if (Request["__EVENTTARGET"] != upArchive.ClientID)
                    return;
            if (string.IsNullOrEmpty(Request["__EVENTARGUMENT"]))
                return;
            else
            {
                string[] arrMonthYear = Request["__EVENTARGUMENT"].Split(';');
                DateTime dateA = new DateTime(Convert.ToInt32(arrMonthYear[2]), Convert.ToInt32(arrMonthYear[1]), Convert.ToInt32(arrMonthYear[0]));
                DateTime begDate = dateA;
                DateTime endDate = dateA.AddDays(1);

                int i = 1;
                List<Archive> data = new List<Archive>();

                DirectoryInfo info = new DirectoryInfo(Server.MapPath(PathVideo));
                List<FileInfo> files = info.GetFiles("*.flv", SearchOption.TopDirectoryOnly).Where(p => p.CreationTime >= begDate).Where(p => p.CreationTime < endDate).ToList();
                foreach (FileInfo file in files)
                {
                    Archive a = new Archive();
                    a.ArchiveType = "stream";
                    a.FileDate = file.CreationTime;
                    a.FileIndex = i;
                    a.FilePath = file.FullName;
                    data.Add(a);
                    ++i;
                }

                info = new DirectoryInfo(Server.MapPath(PathChat));
                files = info.GetFiles("*.txt", SearchOption.TopDirectoryOnly).Where(p => p.CreationTime >= begDate).Where(p => p.CreationTime < endDate).ToList();
                foreach (FileInfo file in files)
                {
                    Archive a = new Archive();
                    a.ArchiveType = "chat";
                    a.FileDate = file.CreationTime;
                    a.FileIndex = i;
                    a.FilePath = file.FullName;
                    data.Add(a);
                    ++i;
                }

                rptArchive.DataSource = data;
                rptArchive.DataBind();
            }
        }

        protected void rptArchive_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Archive rowView = e.Item.DataItem as Archive;
                HtmlGenericControl divVideo = (HtmlGenericControl)e.Item.FindControl("divVideoDialog");
                HtmlGenericControl divTxt = (HtmlGenericControl)e.Item.FindControl("divTxt");
                
                if (rowView.ArchiveType == "stream")
                {
                    divTxt.Visible = false;

                    /*string videoScript = string.Format(@"jwplayer('div_video_{0}').setup({{
                                                                    file: '{1}',
                                                                    width: 278,
                                                                    height: 343,
                                                                    skin: 'glow'
                                                                }});
                                                                jwplayer('div_video_{0}').onPlay(function (event) {{ $('#div_desc_{0}').hide(); }});", e.Item.ItemIndex, PathVideo + rowView.FileName);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "VideoScript" + e.Item.ItemIndex, videoScript, true);*/

                    
                }
                else
                    divVideo.Visible = false;

            }
        }

        
    }
}